<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>20-函数简介</title>
  
    
      <meta 
        property="og:title" 
        content="20-函数简介">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/20-%E5%87%BD%E6%95%B0%E7%AE%80%E4%BB%8B/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2004-03-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="JS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E4%BB%8B%E7%BB%8D"><span class="toc-text">函数的介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E5%AE%9A%E4%B9%89-x2F-%E5%A3%B0%E6%98%8E"><span class="toc-text">函数的定义&#x2F;声明</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E%EF%BC%88%E5%91%BD%E5%90%8D%E5%87%BD%E6%95%B0%EF%BC%89"><span class="toc-text">方式一：函数声明（命名函数）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E5%87%BD%E6%95%B0%E8%A1%A8%E8%BE%BE%E5%BC%8F%EF%BC%88%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0%EF%BC%89"><span class="toc-text">方式二：函数表达式（匿名函数）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%B8%89%EF%BC%9A%E4%BD%BF%E7%94%A8%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0-new-Function"><span class="toc-text">方式三：使用构造函数 new Function()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%8F%E7%BB%93"><span class="toc-text">小结</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E8%B0%83%E7%94%A8"><span class="toc-text">函数的调用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F1%EF%BC%9A%E6%99%AE%E9%80%9A%E5%87%BD%E6%95%B0%E7%9A%84%E8%B0%83%E7%94%A8"><span class="toc-text">方式1：普通函数的调用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F2%EF%BC%9A%E9%80%9A%E8%BF%87%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%96%B9%E6%B3%95%E6%9D%A5%E8%B0%83%E7%94%A8"><span class="toc-text">方式2：通过对象的方法来调用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F3%EF%BC%9A%E7%AB%8B%E5%8D%B3%E6%89%A7%E8%A1%8C%E5%87%BD%E6%95%B0"><span class="toc-text">方式3：立即执行函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F4%EF%BC%9A%E9%80%9A%E8%BF%87%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E6%9D%A5%E8%B0%83%E7%94%A8"><span class="toc-text">方式4：通过构造函数来调用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F5%EF%BC%9A%E7%BB%91%E5%AE%9A%E4%BA%8B%E4%BB%B6%E5%87%BD%E6%95%B0"><span class="toc-text">方式5：绑定事件函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F6%EF%BC%9A%E5%AE%9A%E6%97%B6%E5%99%A8%E5%87%BD%E6%95%B0"><span class="toc-text">方式6：定时器函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E5%8F%82%E6%95%B0%EF%BC%9A%E5%BD%A2%E5%8F%82%E5%92%8C%E5%AE%9E%E5%8F%82"><span class="toc-text">函数的参数：形参和实参</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89"><span class="toc-text">定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BD%A2%E5%8F%82%E5%92%8C%E5%AE%9E%E5%8F%82%E7%9A%84%E4%B8%AA%E6%95%B0"><span class="toc-text">形参和实参的个数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E5%8F%82%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-text">实参的数据类型</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="toc-text">函数的返回值</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#return-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-text">return 关键字</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#break%E3%80%81continue%E3%80%81return-%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">break、continue、return 的区别</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%90%8D%E3%80%81%E5%87%BD%E6%95%B0%E4%BD%93%E5%92%8C%E5%87%BD%E6%95%B0%E5%8A%A0%E8%BD%BD%E9%97%AE%E9%A2%98%EF%BC%88%E9%87%8D%E8%A6%81%EF%BC%8C%E8%AF%B7%E8%AE%B0%E4%BD%8F%EF%BC%89"><span class="toc-text">函数名、函数体和函数加载问题（重要，请记住）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#fn-%E5%92%8C-fn-%E7%9A%84%E5%8C%BA%E5%88%AB%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">fn()  和 fn 的区别【重要】</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%B9%E6%B3%95"><span class="toc-text">方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E6%95%B0%E7%BB%84%E5%AF%B9%E8%B1%A1-arguments"><span class="toc-text">类数组对象 arguments</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89-1"><span class="toc-text">定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#arguments-length-%E8%BF%94%E5%9B%9E%E5%87%BD%E6%95%B0%E5%AE%9E%E5%8F%82%E7%9A%84%E4%B8%AA%E6%95%B0"><span class="toc-text">arguments.length 返回函数实参的个数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#arguments-callee-%E8%BF%94%E5%9B%9E%E6%AD%A3%E5%9C%A8%E6%89%A7%E8%A1%8C%E7%9A%84%E5%87%BD%E6%95%B0"><span class="toc-text">arguments.callee 返回正在执行的函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#arguments-%E5%8F%AF%E4%BB%A5%E4%BF%AE%E6%94%B9%E5%85%83%E7%B4%A0"><span class="toc-text">arguments 可以修改元素</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF%E4%B8%BE%E4%BE%8B"><span class="toc-text">使用场景举例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      20-函数简介
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2004-02-29T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2004-03-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/JS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          JS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>3.6k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/JS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            JS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="函数的介绍"><a href="#函数的介绍" class="headerlink" title="函数的介绍"></a>函数的介绍</h2><p>函数：就是一些功能或语句的<strong>封装</strong>。在需要的时候，通过<strong>调用</strong>的形式，执行这些语句。</p>
<p>补充：</p>
<ul>
<li><p><strong>函数也是一个对象</strong></p>
</li>
<li><p>使用<code>typeof</code>检查一个函数对象时，会返回 function</p>
</li>
</ul>
<p><strong>函数的作用</strong>：</p>
<ul>
<li><p>一次定义，多次调用。将大量重复的语句抽取出来，写在函数里，以后需要这些语句时，可以直接调用函数，避免重复劳动。</p>
</li>
<li><p>简化代码，可读性更强，让编程模块化。高内聚、低耦合。</p>
</li>
</ul>
<p>来看个例子：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;你好&quot;</span>);</span><br><span class="line"><span class="title function_">sayHello</span>();	<span class="comment">// 调用函数</span></span><br><span class="line"><span class="title function_">sayHello</span>();	<span class="comment">// 再调用一次函数</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义函数</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">sayHello</span>(<span class="params"></span>)&#123;</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;欢迎&quot;</span>);</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;welcome&quot;</span>);</span><br><span class="line">&#125;</span><br></pre>

<h2 id="函数的定义-x2F-声明"><a href="#函数的定义-x2F-声明" class="headerlink" title="函数的定义&#x2F;声明"></a>函数的定义&#x2F;声明</h2><p>我们使用<code>function</code>关键字定义函数，中文含义是“函数”、“功能”。可以使用如下方式进行定义。</p>
<h3 id="方式一：函数声明（命名函数）"><a href="#方式一：函数声明（命名函数）" class="headerlink" title="方式一：函数声明（命名函数）"></a>方式一：函数声明（命名函数）</h3><p>使用<code>函数声明</code>来创建一个函数。语法：</p>
<pre class="highlight"><span class="line"><span class="keyword">function</span> 函数名([形参<span class="number">1</span>,形参<span class="number">2.</span>..形参N])&#123;  <span class="comment">// 备注：语法中的中括号，表示“可选”</span></span><br><span class="line">	<span class="comment">// 函数体语句</span></span><br><span class="line">&#125;</span><br></pre>

<p>举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">function</span> <span class="title function_">sum</span>(<span class="params">a, b</span>)&#123;</span><br><span class="line">	<span class="keyword">return</span> a+b;</span><br><span class="line">&#125;</span><br></pre>

<p>解释如下：</p>
<ul>
<li><p>函数名：命名规定和变量的命名规定一样，必须符合JS标识符的命名规则。只能是字母、数字、下划线、美元符号，不能以数字开头。</p>
</li>
<li><p>圆括号里，是形参列表，可选。即使没有形参，也必须书写圆括号。</p>
</li>
<li><p>大括号里，是函数体语句。</p>
</li>
</ul>
<p>PS：在有些编辑器中，方法写完之后，我们在方法的前面输入<code>/**</code>，然后回车，会发现，注释的格式会自动补齐。</p>
<h3 id="方式二：函数表达式（匿名函数）"><a href="#方式二：函数表达式（匿名函数）" class="headerlink" title="方式二：函数表达式（匿名函数）"></a>方式二：函数表达式（匿名函数）</h3><p>使用<code>函数表达式</code>来创建一个函数。语法：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> 变量名  = <span class="keyword">function</span>(<span class="params">[形参<span class="number">1</span>,形参<span class="number">2.</span>..形参N]</span>)&#123;</span><br><span class="line">	语句....</span><br><span class="line">&#125;</span><br></pre>

<p>举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> fun2 = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;我是匿名函数中封装的代码&quot;</span>);</span><br><span class="line">&#125;;</span><br></pre>

<p>解释如下：</p>
<ul>
<li><p>上面的 fun2 是变量名，不是函数名。</p>
</li>
<li><p>函数表达式的声明方式跟声明变量类似，只不过变量里存的是值，而函数表达式里存的是函数。</p>
</li>
<li><p>函数表达式也可以传递参数。</p>
</li>
</ul>
<p>从方式二的举例中可以看出：所谓的“函数表达式”，其实就是将匿名函数赋值给一个变量。因为，一个匿名函数终究还是要给它一个接收对象，进而方便地调用这个函数。</p>
<h3 id="方式三：使用构造函数-new-Function"><a href="#方式三：使用构造函数-new-Function" class="headerlink" title="方式三：使用构造函数 new Function()"></a>方式三：使用构造函数 new Function()</h3><p>使用构造函数<code>new Function()</code>来创建一个对象。这种方式，用的少。</p>
<p>语法：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> 变量名/函数名  = <span class="keyword">new</span> <span class="title class_">Function</span>(<span class="string">&#x27;形参1&#x27;</span>, <span class="string">&#x27;形参2&#x27;</span>, <span class="string">&#x27;函数体&#x27;</span>);</span><br></pre>

<p>注意，Function 里面的参数都必须是<strong>字符串</strong>格式。也就是说，形参也必须放在<strong>字符串</strong>里；函数体也是放在<strong>字符串</strong>里包裹起来，放在 Function 的最后一个参数的位置。</p>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> fun3 = <span class="keyword">new</span> <span class="title class_">Function</span>(<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;console.log(&quot;我是函数内部的内容&quot;);  console.log(a + b);&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="title function_">fun3</span>(<span class="number">1</span>, <span class="number">2</span>); <span class="comment">// 调用函数</span></span><br></pre>

<p>打印结果：</p>
<pre class="highlight"><span class="line">我是函数内部的内容</span><br><span class="line">3</span><br></pre>

<p><strong>分析</strong>：</p>
<p>方式3的写法很少用，原因如下：</p>
<ul>
<li><p>不方便书写：写法过于啰嗦和麻烦。</p>
</li>
<li><p>执行效率较低：首先需要把字符串转换为 js 代码，然后再执行。</p>
</li>
</ul>
<h3 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h3><p>1、<strong>所有的函数，都是 <code>Fuction</code> 的“实例”</strong>（或者说是“实例对象”）。函数本质上都是通过 new Function 得到的。</p>
<p>2、函数既然是实例对象，那么，<strong>函数也属于“对象”</strong>。还可以通过如下特征，来佐证函数属于对象：</p>
<p>（1）我们直接打印某一个函数，比如 <code>console.log(fun2)</code>，发现它的里面有<code>__proto__</code>。（这个是属于原型的知识，后续再讲）</p>
<p>（2）我们还可以打印 <code>console.log(fun2 instanceof Object)</code>，发现打印结果为 <code>true</code>。这说明 fun2 函数就是属于 Object。</p>
<h2 id="函数的调用"><a href="#函数的调用" class="headerlink" title="函数的调用"></a>函数的调用</h2><p>调用函数即：执行函数体中的语句。函数必须要等到被调用时才执行。</p>
<h3 id="方式1：普通函数的调用"><a href="#方式1：普通函数的调用" class="headerlink" title="方式1：普通函数的调用"></a>方式1：普通函数的调用</h3><p>函数调用的语法：</p>
<pre class="highlight"><span class="line"><span class="comment">// 写法1（最常用）</span></span><br><span class="line">函数名();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 写法2</span></span><br><span class="line">函数名.<span class="title function_">call</span>();</span><br></pre>



<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">function</span> <span class="title function_">fn1</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;我是函数体里面的内容1&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fn2</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;我是函数体里面的内容2&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">fn1</span>(); <span class="comment">// 调用函数</span></span><br><span class="line"></span><br><span class="line">fn2.<span class="title function_">call</span>(); <span class="comment">// 调用函数</span></span><br><span class="line"></span><br></pre>

<h3 id="方式2：通过对象的方法来调用"><a href="#方式2：通过对象的方法来调用" class="headerlink" title="方式2：通过对象的方法来调用"></a>方式2：通过对象的方法来调用</h3><pre class="highlight"><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">	<span class="attr">a</span>: <span class="string">&#x27;qianguyihao&#x27;</span>,</span><br><span class="line">	<span class="attr">fn2</span>: <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">		<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;千古壹号，永不止步!&#x27;</span>);</span><br><span class="line">	&#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">obj.<span class="title function_">fn2</span>(); <span class="comment">// 调用函数</span></span><br></pre>

<p>如果一个函数是作为一个对象的属性保存，那么，我们称这个函数是这个对象的<strong>方法</strong>。</p>
<p>PS：关于函数和方法的区别，本文的后续内容里有讲到，可以往下面翻。</p>
<h3 id="方式3：立即执行函数"><a href="#方式3：立即执行函数" class="headerlink" title="方式3：立即执行函数"></a>方式3：立即执行函数</h3><p>代码举例：</p>
<pre class="highlight"><span class="line">(<span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;我是立即执行函数&#x27;</span>);</span><br><span class="line">&#125;)();</span><br><span class="line"></span><br></pre>

<p>立即执行函数在定义后，会自动调用。</p>
<p>PS：关于立即执行函数，本文的后续内容里有讲到，可以往下面翻。</p>
<p>上面讲到的这三种方式，是用得最多的。接下来讲到的三种方式，暂时看不懂也没关系，可以等学完其他的知识点，再回过头来看。</p>
<h3 id="方式4：通过构造函数来调用"><a href="#方式4：通过构造函数来调用" class="headerlink" title="方式4：通过构造函数来调用"></a>方式4：通过构造函数来调用</h3><p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">function</span> <span class="title function_">Fun3</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;千古壹号，永不止步~&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> <span class="title class_">Fun3</span>();</span><br></pre>

<p>这种方式用得不多。</p>
<h3 id="方式5：绑定事件函数"><a href="#方式5：绑定事件函数" class="headerlink" title="方式5：绑定事件函数"></a>方式5：绑定事件函数</h3><p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;btn&quot;</span>&gt;</span>我是按钮，请点击我<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> btn = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&#x27;btn&#x27;</span>);</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//2.绑定事件</span></span></span><br><span class="line"><span class="language-javascript">            btn.<span class="property">onclick</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;点击按钮后，要做的事情&#x27;</span>);</span></span><br><span class="line"><span class="language-javascript">            &#125;;</span></span><br><span class="line"><span class="language-javascript">        </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br></pre>

<p>这里涉及到DOM操作和事件的知识点，后续再讲。</p>
<h3 id="方式6：定时器函数"><a href="#方式6：定时器函数" class="headerlink" title="方式6：定时器函数"></a>方式6：定时器函数</h3><p>代码举例：（每间隔一秒，将 数字 加1）</p>
<pre class="highlight"><span class="line"> <span class="keyword">let</span> num = <span class="number">1</span>;</span><br><span class="line"><span class="built_in">setInterval</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    num ++;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(num);</span><br><span class="line">&#125;, <span class="number">1000</span>);</span><br></pre>

<p>这里涉及到定时器的知识点。</p>
<h2 id="函数的参数：形参和实参"><a href="#函数的参数：形参和实参" class="headerlink" title="函数的参数：形参和实参"></a>函数的参数：形参和实参</h2><h3 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h3><p>函数的参数包括形参和实参。形参是函数内的一些<strong>待定值</strong>。在调用函数时，需传入这些参数的具体值（即实参）。</p>
<p>可以在函数的<code>()</code>中指定一个或多个参数，也可以不指定参数。多个参数之间用英文逗号隔开。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="comment">// a, b 是形参，表示待定值</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">add</span>(<span class="params">a, b</span>) &#123;</span><br><span class="line"><span class="keyword">const</span> sum = a + b;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(sum);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 1, 2 是实参，表示传入的具体值。调用函数时，传入实参</span></span><br><span class="line"><span class="title function_">add</span>(<span class="number">1</span>, <span class="number">2</span>);</span><br></pre>

<p><strong>形参：</strong></p>
<ul>
<li>概念：形式上的参数。定义函数时传递的待定值（此时并不知道是什么值）。</li>
<li>声明形参相当于在函数内部声明了变量，但并不赋值。也可以说，<strong>形参的默认值是 undefined</strong>。</li>
</ul>
<p><strong>实参</strong>：</p>
<ul>
<li>概念：实际上的参数。调用函数时传递的具体值。实参将传递给函数中对应的形参。</li>
</ul>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="comment">// 调用函数</span></span><br><span class="line"><span class="title function_">add</span>(<span class="number">3</span>, <span class="number">4</span>);</span><br><span class="line"><span class="title function_">add</span>(<span class="string">&quot;3&quot;</span>, <span class="number">4</span>);</span><br><span class="line"><span class="title function_">add</span>(<span class="string">&quot;Hello&quot;</span>, <span class="string">&quot;World&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义函数：求和</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">add</span>(<span class="params">a, b</span>) &#123;</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(a + b);</span><br><span class="line">&#125;</span><br></pre>

<p>控制台输出结果：</p>
<pre class="highlight"><span class="line">7</span><br><span class="line">34</span><br><span class="line">helloworld</span><br></pre>

<h3 id="形参和实参的个数"><a href="#形参和实参的个数" class="headerlink" title="形参和实参的个数"></a>形参和实参的个数</h3><p>实际参数和形式参数的个数，可以不同。调用函数时，解析器不会检查实参的数量。</p>
<ul>
<li>如果实参个数 &gt; 形参个数，则末尾的实参是多余的，不会被赋值，因为没有形参能接收它。</li>
<li>如果实参个数 &lt; 形参个数，则末尾的形参是多余的，值是 undefined，因为它没有接收到实参。（undefined参与运算时，表达式的运算结果为NaN）</li>
</ul>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">function</span> <span class="title function_">sum</span>(<span class="params">a, b</span>) &#123;</span><br><span class="line">	<span class="variable language_">console</span>.<span class="title function_">log</span>(a + b);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">sum</span>(<span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line"><span class="title function_">sum</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>);</span><br><span class="line"><span class="title function_">sum</span>(<span class="number">1</span>);</span><br></pre>

<p>打印结果：</p>
<pre class="highlight"><span class="line">3</span><br><span class="line">3</span><br><span class="line">NaN</span><br></pre>

<h3 id="实参的数据类型"><a href="#实参的数据类型" class="headerlink" title="实参的数据类型"></a>实参的数据类型</h3><p>函数的实参可以是任意的数据类型。调用函数时，解析器不会检查实参类型，所以要注意，是否有可能会接收到非法的参数，如果有可能则需要对参数进行类型检查。</p>
<h2 id="函数的返回值"><a href="#函数的返回值" class="headerlink" title="函数的返回值"></a>函数的返回值</h2><h3 id="return-关键字"><a href="#return-关键字" class="headerlink" title="return 关键字"></a>return 关键字</h3><p>函数体内可以没有返回值，也可以根据需要加返回值。语法格式：<code>return 函数的返回值</code>。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">sum</span>(<span class="number">3</span>, <span class="number">4</span>)); <span class="comment">// 将函数的返回值打印出来</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//函数：求和</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">sum</span>(<span class="params">a, b</span>) &#123;</span><br><span class="line">	<span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br></pre>

<p>return关键字的作用既可以是<strong>终止函数</strong>，也可以给函数添加返回值。</p>
<p>解释：</p>
<p>（1）return 后的返回值将会作为函数的执行结果返回，可以定义一个变量，来接收该返回值。</p>
<p>（2）在函数中，return后的语句都不会执行。也就是说，函数在执行完 return 语句之后，会立即退出函数。</p>
<p>（3）如果return语句后不跟任何值，就相当于返回一个undefined</p>
<p>（4）如果函数中不写return，则也会返回undefined</p>
<p>（5）返回值可以是任意的数据类型，可以是对象，也可以是函数。</p>
<p>（6）return 只能返回一个值。如果用逗号隔开多个值，则以最后一个为准。</p>
<h3 id="break、continue、return-的区别"><a href="#break、continue、return-的区别" class="headerlink" title="break、continue、return 的区别"></a>break、continue、return 的区别</h3><ul>
<li><p>break ：结束当前的循环体（如 for、while）</p>
</li>
<li><p>continue ：跳出本次循环，继续执行下次循环（如 for、while）</p>
</li>
<li><p>return ：1、退出循环。2、返回 return 语句中的值，同时结束当前的函数体内的代码，退出当前函数。</p>
</li>
</ul>
<h2 id="函数名、函数体和函数加载问题（重要，请记住）"><a href="#函数名、函数体和函数加载问题（重要，请记住）" class="headerlink" title="函数名、函数体和函数加载问题（重要，请记住）"></a>函数名、函数体和函数加载问题（重要，请记住）</h2><p>我们要记住：<strong>函数名 &#x3D;&#x3D; 整个函数</strong>。举例：</p>
<pre class="highlight"><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(fn) == <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params"></span>)&#123;<span class="title function_">alert</span>(<span class="number">1</span>)&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义fn方法</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params"></span>)&#123;</span><br><span class="line">	<span class="title function_">alert</span>(<span class="number">1</span>)</span><br><span class="line">&#125;;</span><br></pre>

<p>我们知道，当我们在调用一个函数时，通常使用<code>函数()</code>这种格式；可如果，我们是直接使用<code>函数</code>这种格式，它的作用相当于整个函数。</p>
<p><strong>函数的加载问题</strong>：JS加载的时候，只加载函数名，不加载函数体。所以如果想使用内部的成员变量，需要调用函数。</p>
<h3 id="fn-和-fn-的区别【重要】"><a href="#fn-和-fn-的区别【重要】" class="headerlink" title="fn()  和 fn 的区别【重要】"></a>fn()  和 fn 的区别【重要】</h3><ul>
<li><p><code>fn()</code>：调用函数。调用之后，还获取了函数的返回值。</p>
</li>
<li><p><code>fn</code>：函数对象。相当于直接获取了整个函数对象。</p>
</li>
</ul>
<h2 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h2><p>函数也可以成为对象的属性。<strong>如果一个函数是作为一个对象的属性保存，那么，我们称这个函数是这个对象的方法</strong>。</p>
<p>调用这个函数就说调用对象的方法（method）。函数和方法，有什么本质的区别吗？它只是名称上的区别，并没有其他的区别。</p>
<p>函数举例：</p>
<pre class="highlight"><span class="line"><span class="comment">// 调用函数</span></span><br><span class="line"><span class="title function_">fn</span>();</span><br></pre>

<p>方法举例：</p>
<pre class="highlight"><span class="line"><span class="comment">// 调用方法</span></span><br><span class="line">obj.<span class="title function_">fn</span>();</span><br></pre>

<p>我们可以这样说，如果直接是<code>fn()</code>，那就说明是函数调用。如果是<code>XX.fn()</code>的这种形式，那就说明是<strong>方法</strong>调用。</p>
<h2 id="类数组对象-arguments"><a href="#类数组对象-arguments" class="headerlink" title="类数组对象 arguments"></a>类数组对象 arguments</h2><blockquote>
<p>这部分，初学者可能看不懂，可以以后再来看。</p>
</blockquote>
<p>在调用函数时，浏览器每次都会传递进两个隐含的参数：</p>
<ul>
<li><p>1.函数的上下文对象 this</p>
</li>
<li><p>2.<strong>封装实参的对象</strong> arguments</p>
</li>
</ul>
<p>这一段，我们来讲一下 arguments。例如：</p>
<pre class="highlight"><span class="line"><span class="keyword">function</span> <span class="title function_">foo</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">arguments</span>);</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> <span class="variable language_">arguments</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">foo</span>(<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>);</span><br></pre>

<p>打印结果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://img.smyhvae.com/20220725_2000.png" class="lozad post-image"src="https://img.smyhvae.com/20220725_2000.png"></p>
<h3 id="定义-1"><a href="#定义-1" class="headerlink" title="定义"></a>定义</h3><p>函数内的 arguments 是一个<strong>类数组对象</strong>，里面存储的是它接收到的<strong>实参列表</strong>。所有函数都内置了一个 arguments 对象，有个讲究的地方是：只有函数才有arguments。</p>
<p>具体来说，在调用函数时，我们所传递的实参都会在 arguments 中保存。<strong>arguments 代表的是所有实参</strong>。</p>
<p>arguments 的展示形式是一个<strong>伪数组</strong>。意思是，它和数组有点像，但它并不是数组。它具有以下特点：</p>
<ul>
<li><p>可以进行遍历；具有数组的 length 属性，可以获取长度。</p>
</li>
<li><p>可以通过索引（从0开始计数）存储数据、获取和操作数据。比如，我们可以通过索引访问某个实参。</p>
</li>
<li><p>不能调用数组的方法。比如push()、pop() 等方法都没有。</p>
</li>
</ul>
<p>我们看一下 arguments 的使用。</p>
<h3 id="arguments-length-返回函数实参的个数"><a href="#arguments-length-返回函数实参的个数" class="headerlink" title="arguments.length 返回函数实参的个数"></a>arguments.length 返回函数实参的个数</h3><p>arguments.length 可以用来获取<strong>实参的个数</strong>。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="title function_">fn</span>(<span class="number">2</span>, <span class="number">4</span>);</span><br><span class="line"><span class="title function_">fn</span>(<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>);</span><br><span class="line"><span class="title function_">fn</span>(<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params">a, b</span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">arguments</span>);</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(fn.<span class="property">length</span>); <span class="comment">//获取形参的个数</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">arguments</span>.<span class="property">length</span>); <span class="comment">//获取实参的个数</span></span><br><span class="line"></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;----------------&#x27;</span>);</span><br><span class="line">&#125;</span><br></pre>

<p>打印结果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180125_2140.png" class="lozad post-image"src="http://img.smyhvae.com/20180125_2140.png"></p>
<p>此外，即使我们不定义形参，也可以通过 arguments 来获取实参：arguments[0] 表示第一个实参、arguments[1] 表示第二个实参，以此类推。</p>
<p>举例：将传入的实参进行求和，无论实参的个数有多少。代码实现：</p>
<pre class="highlight"><span class="line"><span class="keyword">function</span> <span class="title function_">foo</span>(<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">let</span> sum = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="variable language_">arguments</span>.<span class="property">length</span>; i++) &#123;</span><br><span class="line">    sum += <span class="variable language_">arguments</span>[i];</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> result = <span class="title function_">foo</span>(<span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result);</span><br></pre>


<h3 id="arguments-callee-返回正在执行的函数"><a href="#arguments-callee-返回正在执行的函数" class="headerlink" title="arguments.callee 返回正在执行的函数"></a>arguments.callee 返回正在执行的函数</h3><p>arguments 里边有一个属性叫做 callee，这个属性对应一个函数对象，就是当前正在指向的函数对象。</p>
<pre class="highlight"><span class="line"><span class="keyword">function</span> <span class="title function_">fun</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">arguments</span>.<span class="property">callee</span> == fun); <span class="comment">// 打印结果为true</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="title function_">fun</span>(<span class="string">&#x27;hello&#x27;</span>);</span><br></pre>

<p>在使用函数<strong>递归</strong>调用时，推荐使用 arguments.callee 代替函数名本身。</p>
<h3 id="arguments-可以修改元素"><a href="#arguments-可以修改元素" class="headerlink" title="arguments 可以修改元素"></a>arguments 可以修改元素</h3><p>arguments 还可以<strong>修改元素，但不能改变数组的长度</strong>。举例：</p>
<pre class="highlight"><span class="line"><span class="title function_">fn</span>(<span class="number">2</span>, <span class="number">4</span>);</span><br><span class="line"><span class="title function_">fn</span>(<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>);</span><br><span class="line"><span class="title function_">fn</span>(<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params">a, b</span>) &#123;</span><br><span class="line">    <span class="variable language_">arguments</span>[<span class="number">0</span>] = <span class="number">99</span>; <span class="comment">// 将实参的第一个数改为99</span></span><br><span class="line">    <span class="variable language_">arguments</span>.<span class="title function_">push</span>(<span class="number">8</span>); <span class="comment">// 此方法不通过，因为无法增加元素</span></span><br><span class="line">&#125;</span><br></pre>

<h3 id="使用场景举例"><a href="#使用场景举例" class="headerlink" title="使用场景举例"></a>使用场景举例</h3><p>当我们不确定有多少个参数传递的时候，可以用 <strong>arguments</strong> 来获取。</p>
<p><strong>举例</strong>：利用 arguments 求函数实参中的最大值。</p>
<p>代码实现：</p>
<pre class="highlight"><span class="line"><span class="keyword">function</span> <span class="title function_">getMaxValue</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="keyword">var</span> max = <span class="variable language_">arguments</span>[<span class="number">0</span>];</span><br><span class="line">	<span class="comment">// 通过 arguments 遍历实参</span></span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="variable language_">arguments</span>.<span class="property">length</span>; i++) &#123;</span><br><span class="line">		<span class="keyword">if</span> (max &lt; <span class="variable language_">arguments</span>[i]) &#123;</span><br><span class="line">			max = <span class="variable language_">arguments</span>[i];</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> max;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">getMaxValue</span>(<span class="number">1</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">5</span>));</span><br><span class="line"></span><br></pre>


<h2 id="赞赏作者"><a href="#赞赏作者" class="headerlink" title="赞赏作者"></a>赞赏作者</h2><p>创作不易，你的赞赏和认可，是我更新的最大动力：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://img.smyhvae.com/20220401_1800.jpg" class="lozad post-image"src="https://img.smyhvae.com/20220401_1800.jpg"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/20-%E5%87%BD%E6%95%B0%E7%AE%80%E4%BB%8B/">
            https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/20-%E5%87%BD%E6%95%B0%E7%AE%80%E4%BB%8B/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/18-%E6%95%B0%E7%BB%84%E7%AE%80%E4%BB%8B/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">18-数组简介 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/21-%E9%80%92%E5%BD%92%E5%87%BD%E6%95%B0/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">21-递归函数 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E4%BB%8B%E7%BB%8D"><span class="toc-text">函数的介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E5%AE%9A%E4%B9%89-x2F-%E5%A3%B0%E6%98%8E"><span class="toc-text">函数的定义&#x2F;声明</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E%EF%BC%88%E5%91%BD%E5%90%8D%E5%87%BD%E6%95%B0%EF%BC%89"><span class="toc-text">方式一：函数声明（命名函数）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E5%87%BD%E6%95%B0%E8%A1%A8%E8%BE%BE%E5%BC%8F%EF%BC%88%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0%EF%BC%89"><span class="toc-text">方式二：函数表达式（匿名函数）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%B8%89%EF%BC%9A%E4%BD%BF%E7%94%A8%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0-new-Function"><span class="toc-text">方式三：使用构造函数 new Function()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%8F%E7%BB%93"><span class="toc-text">小结</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E8%B0%83%E7%94%A8"><span class="toc-text">函数的调用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F1%EF%BC%9A%E6%99%AE%E9%80%9A%E5%87%BD%E6%95%B0%E7%9A%84%E8%B0%83%E7%94%A8"><span class="toc-text">方式1：普通函数的调用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F2%EF%BC%9A%E9%80%9A%E8%BF%87%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%96%B9%E6%B3%95%E6%9D%A5%E8%B0%83%E7%94%A8"><span class="toc-text">方式2：通过对象的方法来调用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F3%EF%BC%9A%E7%AB%8B%E5%8D%B3%E6%89%A7%E8%A1%8C%E5%87%BD%E6%95%B0"><span class="toc-text">方式3：立即执行函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F4%EF%BC%9A%E9%80%9A%E8%BF%87%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E6%9D%A5%E8%B0%83%E7%94%A8"><span class="toc-text">方式4：通过构造函数来调用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F5%EF%BC%9A%E7%BB%91%E5%AE%9A%E4%BA%8B%E4%BB%B6%E5%87%BD%E6%95%B0"><span class="toc-text">方式5：绑定事件函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F6%EF%BC%9A%E5%AE%9A%E6%97%B6%E5%99%A8%E5%87%BD%E6%95%B0"><span class="toc-text">方式6：定时器函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E5%8F%82%E6%95%B0%EF%BC%9A%E5%BD%A2%E5%8F%82%E5%92%8C%E5%AE%9E%E5%8F%82"><span class="toc-text">函数的参数：形参和实参</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89"><span class="toc-text">定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BD%A2%E5%8F%82%E5%92%8C%E5%AE%9E%E5%8F%82%E7%9A%84%E4%B8%AA%E6%95%B0"><span class="toc-text">形参和实参的个数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E5%8F%82%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-text">实参的数据类型</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="toc-text">函数的返回值</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#return-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-text">return 关键字</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#break%E3%80%81continue%E3%80%81return-%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">break、continue、return 的区别</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%90%8D%E3%80%81%E5%87%BD%E6%95%B0%E4%BD%93%E5%92%8C%E5%87%BD%E6%95%B0%E5%8A%A0%E8%BD%BD%E9%97%AE%E9%A2%98%EF%BC%88%E9%87%8D%E8%A6%81%EF%BC%8C%E8%AF%B7%E8%AE%B0%E4%BD%8F%EF%BC%89"><span class="toc-text">函数名、函数体和函数加载问题（重要，请记住）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#fn-%E5%92%8C-fn-%E7%9A%84%E5%8C%BA%E5%88%AB%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">fn()  和 fn 的区别【重要】</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%B9%E6%B3%95"><span class="toc-text">方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E6%95%B0%E7%BB%84%E5%AF%B9%E8%B1%A1-arguments"><span class="toc-text">类数组对象 arguments</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89-1"><span class="toc-text">定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#arguments-length-%E8%BF%94%E5%9B%9E%E5%87%BD%E6%95%B0%E5%AE%9E%E5%8F%82%E7%9A%84%E4%B8%AA%E6%95%B0"><span class="toc-text">arguments.length 返回函数实参的个数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#arguments-callee-%E8%BF%94%E5%9B%9E%E6%AD%A3%E5%9C%A8%E6%89%A7%E8%A1%8C%E7%9A%84%E5%87%BD%E6%95%B0"><span class="toc-text">arguments.callee 返回正在执行的函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#arguments-%E5%8F%AF%E4%BB%A5%E4%BF%AE%E6%94%B9%E5%85%83%E7%B4%A0"><span class="toc-text">arguments 可以修改元素</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF%E4%B8%BE%E4%BE%8B"><span class="toc-text">使用场景举例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E4%BB%8B%E7%BB%8D"><span class="toc-text">函数的介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E5%AE%9A%E4%B9%89-x2F-%E5%A3%B0%E6%98%8E"><span class="toc-text">函数的定义&#x2F;声明</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E%EF%BC%88%E5%91%BD%E5%90%8D%E5%87%BD%E6%95%B0%EF%BC%89"><span class="toc-text">方式一：函数声明（命名函数）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9A%E5%87%BD%E6%95%B0%E8%A1%A8%E8%BE%BE%E5%BC%8F%EF%BC%88%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0%EF%BC%89"><span class="toc-text">方式二：函数表达式（匿名函数）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%B8%89%EF%BC%9A%E4%BD%BF%E7%94%A8%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0-new-Function"><span class="toc-text">方式三：使用构造函数 new Function()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%8F%E7%BB%93"><span class="toc-text">小结</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E8%B0%83%E7%94%A8"><span class="toc-text">函数的调用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F1%EF%BC%9A%E6%99%AE%E9%80%9A%E5%87%BD%E6%95%B0%E7%9A%84%E8%B0%83%E7%94%A8"><span class="toc-text">方式1：普通函数的调用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F2%EF%BC%9A%E9%80%9A%E8%BF%87%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%96%B9%E6%B3%95%E6%9D%A5%E8%B0%83%E7%94%A8"><span class="toc-text">方式2：通过对象的方法来调用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F3%EF%BC%9A%E7%AB%8B%E5%8D%B3%E6%89%A7%E8%A1%8C%E5%87%BD%E6%95%B0"><span class="toc-text">方式3：立即执行函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F4%EF%BC%9A%E9%80%9A%E8%BF%87%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E6%9D%A5%E8%B0%83%E7%94%A8"><span class="toc-text">方式4：通过构造函数来调用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F5%EF%BC%9A%E7%BB%91%E5%AE%9A%E4%BA%8B%E4%BB%B6%E5%87%BD%E6%95%B0"><span class="toc-text">方式5：绑定事件函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F6%EF%BC%9A%E5%AE%9A%E6%97%B6%E5%99%A8%E5%87%BD%E6%95%B0"><span class="toc-text">方式6：定时器函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E5%8F%82%E6%95%B0%EF%BC%9A%E5%BD%A2%E5%8F%82%E5%92%8C%E5%AE%9E%E5%8F%82"><span class="toc-text">函数的参数：形参和实参</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89"><span class="toc-text">定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BD%A2%E5%8F%82%E5%92%8C%E5%AE%9E%E5%8F%82%E7%9A%84%E4%B8%AA%E6%95%B0"><span class="toc-text">形参和实参的个数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E5%8F%82%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-text">实参的数据类型</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="toc-text">函数的返回值</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#return-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-text">return 关键字</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#break%E3%80%81continue%E3%80%81return-%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-text">break、continue、return 的区别</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E5%90%8D%E3%80%81%E5%87%BD%E6%95%B0%E4%BD%93%E5%92%8C%E5%87%BD%E6%95%B0%E5%8A%A0%E8%BD%BD%E9%97%AE%E9%A2%98%EF%BC%88%E9%87%8D%E8%A6%81%EF%BC%8C%E8%AF%B7%E8%AE%B0%E4%BD%8F%EF%BC%89"><span class="toc-text">函数名、函数体和函数加载问题（重要，请记住）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#fn-%E5%92%8C-fn-%E7%9A%84%E5%8C%BA%E5%88%AB%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">fn()  和 fn 的区别【重要】</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%B9%E6%B3%95"><span class="toc-text">方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E6%95%B0%E7%BB%84%E5%AF%B9%E8%B1%A1-arguments"><span class="toc-text">类数组对象 arguments</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89-1"><span class="toc-text">定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#arguments-length-%E8%BF%94%E5%9B%9E%E5%87%BD%E6%95%B0%E5%AE%9E%E5%8F%82%E7%9A%84%E4%B8%AA%E6%95%B0"><span class="toc-text">arguments.length 返回函数实参的个数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#arguments-callee-%E8%BF%94%E5%9B%9E%E6%AD%A3%E5%9C%A8%E6%89%A7%E8%A1%8C%E7%9A%84%E5%87%BD%E6%95%B0"><span class="toc-text">arguments.callee 返回正在执行的函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#arguments-%E5%8F%AF%E4%BB%A5%E4%BF%AE%E6%94%B9%E5%85%83%E7%B4%A0"><span class="toc-text">arguments 可以修改元素</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF%E4%B8%BE%E4%BE%8B"><span class="toc-text">使用场景举例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
